package com.cz.mes;

import cn.hutool.core.io.FileUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.cz.token.LoginService;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 清库存
 */
public class ClearStock {
    public static void main(String[] args) {
        String filePath = "C:\\Users\\admin\\Desktop\\清除库存占用.txt";
        List<String> lines = FileUtil.readLines(filePath, "UTF-8");
        LoginService loginService = new LoginService();
        String loginToken = loginService.getProdToken();

        String dbGroup = "mes-prod-master";
        lines.forEach(bom -> {
            System.out.println("bom单号:" + bom);
            String sql = "select pmd.* from production_material pm INNER JOIN production_material_detail pmd on pm.id = pmd.production_material_id where pm.bill_no=:billNo";
            Map<String,Object> paramMap = new HashMap<>();
            paramMap.put("billNo",bom);
            List<Entity> result = new ArrayList<>();
            try {
                result = Db.use(dbGroup).query(sql, paramMap);
            } catch (SQLException e) {
                e.printStackTrace();
            }

            for (Entity entity : result) {
                Integer id = entity.getInt("id");
                String itemSkuCode = entity.getStr("item_sku_code");
                String outbound_apply_note_id = entity.getStr("outbound_apply_note_id");
                String outbound_apply_detail_note_id = entity.getStr("outbound_apply_detail_note_id");
                System.out.println("id:" + id + ",itemSkuCode:" + itemSkuCode + ",outbound_apply_note_id:" + outbound_apply_note_id + ",outbound_apply_detail_note_id:" + outbound_apply_detail_note_id);

                Map<String, Object> updateMap = new HashMap<>();
                updateMap.put("outbound_apply_note_id", outbound_apply_note_id);
                updateMap.put("outbound_apply_detail_note_id", outbound_apply_detail_note_id);
                String updateSql = "update production_material_detail set outbound_apply_note_id =:outbound_apply_note_id,outbound_apply_detail_note_id =:outbound_apply_detail_note_id where id = 13597";
                int i = 0;
                try {
                    i = Db.use(dbGroup).execute(updateSql, updateMap);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                System.out.println("更新结果:" + i);

                if(i > 0){

                    Map<String, Object> releaseMap = new HashMap<>();
                    List<Integer> productionMaterialDetailIds = new ArrayList<>();
                    productionMaterialDetailIds.add(13597);
                    releaseMap.put("productionMaterialDetailIds", productionMaterialDetailIds);
                    releaseMap.put("checkProductionRequisition", false);
                    clearStock(releaseMap,loginToken);
                    System.out.println("清空成功 -----------------------------------------------------------------------------------------------------------");
                }
            }

        });


    }

    public static void clearStock(Map<String, Object> releaseMap,String loginToken) {
        String url = "http://gateway.prod.ant2world.com/at-mes-api/productionMaterial/releaseReservation";
        HttpRequest request = HttpUtil.createPost(url);


        String token = "Bearer "+loginToken;

        request.header("Authorization", token);
        String data = JSONUtil.toJsonStr(releaseMap);
        System.out.println("请求参数:" + data);
        request.body(data, "application/json");
        HttpResponse response = request.execute();
        if (response.isOk()) {
            System.out.println(response.body());
        } else {
            System.out.println("error:" + response.getStatus());
        }
    }
}
